﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;
using System.Xml.Linq;

namespace Framework.WebmasterTool
{
    public partial class Supporter : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadGroupSupport();
            }
        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.SupportOnline.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                Panel1.Visible = false;
            }
        }

        public void LoadGroupSupport()
        {
            XDocument xDocGroupSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_GroupSupport_Xml));
            var listXEGroupSupport = xDocGroupSupport.Descendants("GroupSupport").Select(gs => new { ID = gs.Element("id").Value.Trim(), Name = gs.Element("name").Value.Trim(), Index = gs.Element("index").Value.Trim() }).ToList();
            grvGroup.DataSource = listXEGroupSupport;
            grvGroup.DataBind();

            ddlGroupSupport.Items.Clear();
            ddlGroupSupport.Items.Add(new ListItem("Không group", "0"));
            ddlGroupSupport.DataValueField = "ID";
            ddlGroupSupport.DataTextField = "Name";
            ddlGroupSupport.DataSource = listXEGroupSupport;
            ddlGroupSupport.DataBind();

            ddlGroupHotline.Items.Clear();
            ddlGroupHotline.Items.Add(new ListItem("Không group", "0"));
            ddlGroupHotline.DataValueField = "ID";
            ddlGroupHotline.DataTextField = "Name";
            ddlGroupHotline.DataSource = listXEGroupSupport;
            ddlGroupHotline.DataBind();

            ddlGroupContact.Items.Clear();
            ddlGroupContact.Items.Add(new ListItem("Không group", "0"));
            ddlGroupContact.DataValueField = "ID";
            ddlGroupContact.DataTextField = "Name";
            ddlGroupContact.DataSource = listXEGroupSupport;
            ddlGroupContact.DataBind();

            LoadSupport();
            LoadHotline();
            LoadContact();
        }

        protected void grvGroup_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvGroup.PageIndex = e.NewPageIndex;
            LoadGroupSupport();
        }

        protected void btnGroupSave_Click(object sender, EventArgs e)
        {
            XDocument xDocGroupSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_GroupSupport_Xml));
            if (btnGroupSave.CommandName == "Insert")
            {
                XElement xEGroupMaxID = xDocGroupSupport.Descendants("GroupSupport").OrderByDescending(gs => (int)gs.Element("id")).FirstOrDefault();
                int id = 0;
                if (xEGroupMaxID == null)
                {
                    id = 1;
                }
                else
                {
                    id = int.Parse(xEGroupMaxID.Element("id").Value) + 1;
                }
                xDocGroupSupport.Root.Add(new XElement("GroupSupport",
                    new XElement("id", id.ToString()),
                    new XElement("name", txtGroup.Text.Trim()),
                    new XElement("name_en", txtGroup_en.Text.Trim()),
                    new XElement("index", txtGroupIndex.Text.Trim())));
                xDocGroupSupport.Save(Server.MapPath(ConfigPaths.Path_GroupSupport_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Insert, "Tạo nhóm hỗ trợ: " + txtGroup.Text.Trim(), "", "", "", "",
                        "", "", "", "");

                LoadGroupSupport();
                ClearGroupToInsert();
                lblMsg.Text = "Thêm nhóm mới thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {
                int id = int.Parse(btnGroupSave.CommandArgument);
                XElement xEGroup = xDocGroupSupport.Descendants("GroupSupport").Where(gs => gs.Element("id").Value == id.ToString()).FirstOrDefault();
                string oldTitle = xEGroup.Element("name").Value;
                xEGroup.Element("name").Value = txtGroup.Text.Trim();
                xEGroup.Element("name_en").Value = txtGroup_en.Text.Trim();
                xEGroup.Element("index").Value = txtGroupIndex.Text.Trim();
                xDocGroupSupport.Save(Server.MapPath(ConfigPaths.Path_GroupSupport_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Update, "Cập nhật nhóm hỗ trợ: " + oldTitle + " - " + txtGroup.Text.Trim(), "", "", "", "",
                        "", "", "", "");

                LoadGroupSupport();
                ClearGroupToInsert();
                lblMsg.Text = "Sửa nhóm mới thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        protected void btnGroupNew_Click(object sender, EventArgs e)
        {
            ClearGroupToInsert();
        }

        private void ClearGroupToInsert()
        {
            txtGroup.Text = txtGroup_en.Text = txtGroupIndex.Text = "";
            btnGroupNew.Visible = btnGroupDelete.Visible = false;
            btnGroupSave.CommandName = "Insert";
            ClearSupportToInsert();
            ClearHotlineToInsert();
        }

        protected void imbGroupEdit_Command(object sender, CommandEventArgs e)
        {
            lblMsg.Text = "";
            int id = int.Parse(e.CommandArgument.ToString());
            LoadGroupSupportDetails(id);
        }

        public void LoadGroupSupportDetails(int id)
        {
            XDocument xDocGroupSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_GroupSupport_Xml));
            XElement xEGroupSupport = xDocGroupSupport.Descendants("GroupSupport").Where(gs => gs.Element("id").Value == id.ToString()).FirstOrDefault();
            txtGroup.Text = xEGroupSupport.Element("name").Value.Trim();
            txtGroup_en.Text = xEGroupSupport.Element("name_en").Value.Trim();
            txtGroupIndex.Text = xEGroupSupport.Element("index").Value.Trim();
            btnGroupSave.CommandArgument = id.ToString();
            btnGroupSave.CommandName = "Update";
            btnGroupNew.Visible = btnGroupDelete.Visible = true;
        }

        protected void btnGroupDelete_Click(object sender, EventArgs e)
        {
            XDocument xDocGroupSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_GroupSupport_Xml));
            int id = int.Parse(btnGroupSave.CommandArgument);
            XElement xEGroupSupport = xDocGroupSupport.Descendants("GroupSupport").Where(gs => gs.Element("id").Value == id.ToString()).FirstOrDefault();
            XDocument xDocSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_Support_Xml));
            List<XElement> listXESupport = xDocSupport.Descendants("supporter").Where(sp => sp.Element("group").Value == xEGroupSupport.Element("id").Value).ToList();
            XDocument xDocHotline = XDocument.Load(Server.MapPath(ConfigPaths.Path_Hotline_Xml));
            List<XElement> listXEHotline = xDocHotline.Descendants("HotLine").Where(hl => hl.Element("group").Value == xEGroupSupport.Element("id").Value).ToList();
            XDocument xDocContact = XDocument.Load(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));
            List<XElement> listXEContact = xDocContact.Descendants("contact_group").Where(cg => cg.Element("group").Value == xEGroupSupport.Element("id").Value).ToList();

            //create db log
            CreateDBLog(null, null, TypeAction.Delete, "Xoá nhóm hỗ trợ: " + xEGroupSupport.Element("name").Value, "", "", "", "",
                    "", "", "", "");

            xEGroupSupport.Remove();
            xDocGroupSupport.Save(Server.MapPath(ConfigPaths.Path_GroupSupport_Xml));
            listXESupport.Remove();
            xDocSupport.Save(Server.MapPath(ConfigPaths.Path_Support_Xml));
            listXEHotline.Remove();
            xDocHotline.Save(Server.MapPath(ConfigPaths.Path_Hotline_Xml));
            listXEContact.Remove();
            xDocContact.Save(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));
            LoadGroupSupport();
            ClearGroupToInsert();
            ClearSupportToInsert();
            ClearHotlineToInsert();
            ClearContactToInsert();
            lblMsg.Text = "Xoá nhóm thành công";
            lblMsg.ForeColor = System.Drawing.Color.Blue;
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        public void LoadSupport()
        {
            XDocument xDocSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_Support_Xml));
            var listXESupport = xDocSupport.Descendants("supporter").Select(sp => new { ID = sp.Element("id").Value.Trim(), Nickname = sp.Element("nickname").Value.Trim(), ShowName = sp.Element("showname").Value.Trim(), TypeSupport = sp.Element("type").Value.Trim(), Index = sp.Element("index").Value.Trim() }).ToList();
            grvSupporters.DataSource = listXESupport;
            grvSupporters.DataBind();
        }

        protected void grvSupporters_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvSupporters.PageIndex = e.NewPageIndex;
            LoadSupport();
        }

        protected void btnSupportSave_Click(object sender, EventArgs e)
        {
            XDocument xDocSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_Support_Xml));
            if (btnSupportSave.CommandName == "Insert")
            {
                XElement xESupport = xDocSupport.Descendants("supporter").OrderByDescending(sp => (int)sp.Element("id")).FirstOrDefault();
                int id;
                if (xESupport == null)
                {
                    id = 0;
                }
                else
                {
                    id = int.Parse(xESupport.Element("id").Value) + 1;
                }
                xDocSupport.Root.Add(new XElement("supporter",
                    new XElement("index", txtSupportIndex.Text),
                    new XElement("showname", txtSupportDescription.Text.Trim()),
                    new XElement("nickname", txtNickname.Text.Trim()),
                    new XElement("id", id),
                    new XElement("showname_en", txtSupportDescription_en.Text.Trim()),
                    new XElement("type", ddlTypeSupport.SelectedValue),
                    new XElement("group", ddlGroupSupport.SelectedValue)));
                xDocSupport.Save(Server.MapPath(ConfigPaths.Path_Support_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Insert, "Tạo nick hỗ trợ: " + txtNickname.Text.Trim() + " - " + txtSupportDescription.Text.Trim() + " - " + ddlTypeSupport.SelectedValue, "", "", "", "",
                        "", "", "", "");

                LoadSupport();
                ClearSupportToInsert();
                lblMsg.Text = "Thêm hỗ trợ online mới thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {
                int id = int.Parse(btnSupportSave.CommandArgument.ToString());
                XElement xESupport = xDocSupport.Descendants("supporter").Where(sp => sp.Element("id").Value == id.ToString()).FirstOrDefault();
                xESupport.Element("index").Value = txtSupportIndex.Text;
                string oldTitle = xESupport.Element("showname").Value;
                xESupport.Element("showname").Value = txtSupportDescription.Text.Trim();
                string oldNick = xESupport.Element("nickname").Value;
                xESupport.Element("nickname").Value = txtNickname.Text.Trim();
                xESupport.Element("showname_en").Value = txtSupportDescription_en.Text.Trim();
                xESupport.Element("type").Value = ddlTypeSupport.SelectedValue.Trim();
                xESupport.Element("group").Value = ddlGroupSupport.SelectedValue.Trim();
                xDocSupport.Save(Server.MapPath(ConfigPaths.Path_Support_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Update, "Cập nhật nick hỗ trợ: (" + oldNick + " - " + txtNickname.Text.Trim() + ") " + "(" + oldTitle + " - " + txtSupportDescription.Text.Trim() + ") " + ddlTypeSupport.SelectedValue.Trim(), "", "", "", "",
                        "", "", "", "");

                LoadSupport();
                ClearSupportToInsert();
                lblMsg.Text = "Sửa hỗ trợ online mới thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        protected void btnSupportNew_Click(object sender, EventArgs e)
        {
            ClearSupportToInsert();
        }

        private void ClearSupportToInsert()
        {
            txtNickname.Text = txtSupportDescription.Text = txtSupportDescription_en.Text = txtSupportIndex.Text = "";
            ddlTypeSupport.SelectedIndex = ddlGroupSupport.SelectedIndex = 0;
            btnSupportNew.Visible = btnSupportDelete.Visible = false;
            btnSupportSave.CommandName = "Insert";
        }

        protected void imbSupportEdit_Command(object sender, CommandEventArgs e)
        {
            lblMsg.Text = "";
            int id = int.Parse(e.CommandArgument.ToString());
            LoadSupportDetails(id);
        }

        public void LoadSupportDetails(int id)
        {
            XDocument xDocSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_Support_Xml));
            XElement xESupport = xDocSupport.Descendants("supporter").Where(sp => sp.Element("id").Value == id.ToString()).FirstOrDefault();
            txtNickname.Text = xESupport.Element("nickname").Value.Trim();
            txtSupportDescription.Text = xESupport.Element("showname").Value.Trim();
            txtSupportDescription_en.Text = xESupport.Element("showname_en").Value.Trim();
            txtSupportIndex.Text = xESupport.Element("index").Value.Trim();
            ddlTypeSupport.SelectedValue = xESupport.Element("type").Value.Trim();
            ddlGroupSupport.SelectedValue = xESupport.Element("group").Value.Trim();
            btnSupportSave.CommandArgument = id.ToString();
            btnSupportSave.CommandName = "Update";
            btnSupportNew.Visible = btnSupportDelete.Visible = true;
        }

        protected void btnSupportDelete_Click(object sender, EventArgs e)
        {
            XDocument xDocSupport = XDocument.Load(Server.MapPath(ConfigPaths.Path_Support_Xml));
            int id = int.Parse(btnSupportSave.CommandArgument);
            XElement xESupport = xDocSupport.Descendants("supporter").Where(sp => sp.Element("id").Value == id.ToString()).FirstOrDefault();

            //create db log
            CreateDBLog(null, null, TypeAction.Delete, "Xoá nick hỗ trợ: " + xESupport.Element("nickname").Value, "", "", "", "",
                    "", "", "", "");

            xESupport.Remove();
            xDocSupport.Save(Server.MapPath(ConfigPaths.Path_Support_Xml));
            LoadSupport();
            ClearSupportToInsert();
            lblMsg.Text = "Xoá hỗ trợ online thành công";
            lblMsg.ForeColor = System.Drawing.Color.Blue;
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        public void LoadHotline()
        {
            XDocument xDocHotline = XDocument.Load(Server.MapPath(ConfigPaths.Path_Hotline_Xml));
            var listXEHoline = xDocHotline.Descendants("HotLine").Select(hl => new { ID = hl.Element("id").Value.Trim(), Number = hl.Element("number").Value.Trim(), Description = hl.Element("description").Value.Trim(), Index = hl.Element("index").Value.Trim() }).ToList();
            grvHotLine.DataSource = listXEHoline;
            grvHotLine.DataBind();
        }

        protected void grvHotLine_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvHotLine.PageIndex = e.NewPageIndex;
            LoadHotline();
        }

        protected void btnHotlineSave_Click(object sender, EventArgs e)
        {
            XDocument xDocHotline = XDocument.Load(Server.MapPath(ConfigPaths.Path_Hotline_Xml));
            if (btnHotlineSave.CommandName == "Insert")
            {
                XElement xEHotline = xDocHotline.Descendants("HotLine").OrderByDescending(hl => (int)hl.Element("id")).FirstOrDefault();
                int id;
                if (xEHotline == null)
                {
                    id = 0;
                }
                else
                {
                    id = int.Parse(xEHotline.Element("id").Value) + 1;
                }
                xDocHotline.Root.Add(new XElement("HotLine",
                    new XElement("index", txtHotlineIndex.Text.Trim()),
                    new XElement("id", id),
                    new XElement("number", txtPhone.Text.Trim()),
                    new XElement("description", txtHotlineDescription.Text.Trim()),
                    new XElement("description_en", txtHotlineDescription_en.Text.Trim()),
                    new XElement("group", ddlGroupHotline.SelectedValue.Trim())));
                xDocHotline.Save(Server.MapPath(ConfigPaths.Path_Hotline_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Insert, "Tạo hotline hỗ trợ: " + txtPhone.Text.Trim() + " - " + txtHotlineDescription.Text.Trim(), "", "", "", "",
                        "", "", "", "");

                LoadHotline();
                ClearHotlineToInsert();
                lblMsg.Text = "Thêm hotline mới thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {
                int id = int.Parse(btnHotlineSave.CommandArgument.ToString());
                XElement xEHotline = xDocHotline.Descendants("HotLine").Where(hl => hl.Element("id").Value == id.ToString()).FirstOrDefault();
                xEHotline.Element("index").Value = txtHotlineIndex.Text.Trim();
                string oldNumber = xEHotline.Element("number").Value;
                xEHotline.Element("number").Value = txtPhone.Text.Trim();
                string oldTitle = xEHotline.Element("description").Value;
                xEHotline.Element("description").Value = txtHotlineDescription.Text.Trim();
                xEHotline.Element("description_en").Value = txtHotlineDescription_en.Text.Trim();
                xEHotline.Element("group").Value = ddlGroupHotline.SelectedValue.Trim();
                xDocHotline.Save(Server.MapPath(ConfigPaths.Path_Hotline_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Update, "Cập nhật hotline hỗ trợ: (" + oldNumber + " - " + txtPhone.Text.Trim() + ") " + "(" + oldTitle + " - " + txtHotlineDescription.Text.Trim() + ") ", "", "", "", "",
                        "", "", "", "");

                LoadHotline();
                ClearHotlineToInsert();
                lblMsg.Text = "Sửa hotline thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        protected void btnHotlineNew_Click(object sender, EventArgs e)
        {
            ClearHotlineToInsert();
        }

        private void ClearHotlineToInsert()
        {
            txtPhone.Text = txtHotlineIndex.Text = txtHotlineDescription.Text = txtHotlineDescription_en.Text = "";
            ddlGroupHotline.SelectedIndex = 0;
            btnHotlineNew.Visible = btnHotlineDelete.Visible = false;
            btnHotlineSave.CommandName = "Insert";
        }

        protected void imbHotlineEdit_Command(object sender, CommandEventArgs e)
        {
            lblMsg.Text = "";
            int id = int.Parse(e.CommandArgument.ToString());
            LoadHotlineDetails(id);
        }

        public void LoadHotlineDetails(int id)
        {
            XDocument xDocHotline = XDocument.Load(Server.MapPath(ConfigPaths.Path_Hotline_Xml));
            XElement xEHotline = xDocHotline.Descendants("HotLine").Where(hl => hl.Element("id").Value == id.ToString()).FirstOrDefault();
            txtHotlineIndex.Text = xEHotline.Element("index").Value.Trim();
            txtPhone.Text = xEHotline.Element("number").Value.Trim();
            txtHotlineDescription.Text = xEHotline.Element("description").Value.Trim();
            txtHotlineDescription_en.Text = xEHotline.Element("description_en").Value.Trim();
            ddlGroupHotline.SelectedValue = xEHotline.Element("group").Value.Trim();
            btnHotlineSave.CommandArgument = id.ToString();
            btnHotlineSave.CommandName = "Update";
            btnHotlineNew.Visible = btnHotlineDelete.Visible = true;
        }

        protected void btnHotlineDelete_Click(object sender, EventArgs e)
        {
            XDocument xDocHotline = XDocument.Load(Server.MapPath(ConfigPaths.Path_Hotline_Xml));
            int id = int.Parse(btnHotlineSave.CommandArgument);
            XElement xEHotline = xDocHotline.Descendants("HotLine").Where(hl => hl.Element("id").Value == id.ToString()).FirstOrDefault();

            //create db log
            CreateDBLog(null, null, TypeAction.Delete, "Xoá hotline hỗ trợ: " + xEHotline.Element("number").Value, "", "", "", "",
                    "", "", "", "");

            xEHotline.Remove();
            xDocHotline.Save(Server.MapPath(ConfigPaths.Path_Hotline_Xml));
            LoadHotline();
            ClearHotlineToInsert();
            lblMsg.Text = "Xoá hotline thành công";
            lblMsg.ForeColor = System.Drawing.Color.Blue;
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        public void LoadContact()
        {
            XDocument xDocContact = XDocument.Load(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));
            var listXEContact = xDocContact.Descendants("contact_group").Select(sp => new { ID = sp.Element("id").Value.Trim(), Mail = sp.Element("mail").Value.Trim(), ShowName = sp.Element("showname").Value.Trim(), Index = sp.Element("index").Value.Trim() }).ToList();
            grvContactGroup.DataSource = listXEContact;
            grvContactGroup.DataBind();
        }

        protected void grvContactGroup_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvContactGroup.PageIndex = e.NewPageIndex;
            LoadContact();
        }

        protected void btnSaveContactGroup_Click(object sender, EventArgs e)
        {
            XDocument xDocContact = XDocument.Load(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));
            if (btnSaveContactGroup.CommandName == "Insert")
            {
                XElement xEContact = xDocContact.Descendants("contact_group").OrderByDescending(cg => (int)cg.Element("id")).FirstOrDefault();
                int id;
                if (xEContact == null)
                {
                    id = 0;
                }
                else
                {
                    id = int.Parse(xEContact.Element("id").Value) + 1;
                }
                xDocContact.Root.Add(new XElement("contact_group",
                    new XElement("id", id),
                    new XElement("index", txtDisplayOrderContact.Text),
                    new XElement("showname", txtDescriptionContact.Text.Trim()),
                    new XElement("showname_en", txtDescriptionContact_en.Text.Trim()),
                    new XElement("mail", txtMailContact.Text.Trim()),
                    new XElement("group", ddlGroupContact.SelectedValue)));
                xDocContact.Save(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Insert, "Tạo mail hỗ trợ: " + txtMailContact.Text.Trim() + " - " + txtDescriptionContact.Text.Trim(), "", "", "", "",
                        "", "", "", "");

                LoadContact();
                ClearContactToInsert();
                lblMsg.Text = "Thêm liên hệ nhóm mới thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {
                int id = int.Parse(btnSaveContactGroup.CommandArgument.ToString());
                XElement xEContact = xDocContact.Descendants("contact_group").Where(cg => cg.Element("id").Value == id.ToString()).FirstOrDefault();
                xEContact.Element("index").Value = txtDisplayOrderContact.Text;
                string oldTitle = xEContact.Element("showname").Value;
                xEContact.Element("showname").Value = txtDescriptionContact.Text.Trim();
                string oldMail = xEContact.Element("mail").Value;
                xEContact.Element("mail").Value = txtMailContact.Text.Trim();
                xEContact.Element("showname_en").Value = txtDescriptionContact_en.Text.Trim();
                xEContact.Element("group").Value = ddlGroupContact.SelectedValue.Trim();
                xDocContact.Save(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));

                //create db log
                CreateDBLog(null, null, TypeAction.Update, "Cập nhật mail hỗ trợ: (" + oldMail + " - " + txtMailContact.Text.Trim() + ") " + "(" + oldTitle + " - " + txtDescriptionContact.Text.Trim() + ") ", "", "", "", "",
                        "", "", "", "");

                LoadContact();
                ClearContactToInsert();
                lblMsg.Text = "Sửa liên hệ nhóm mới thành công";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        protected void btnNewContactGroup_Click(object sender, EventArgs e)
        {
            ClearContactToInsert();
        }

        private void ClearContactToInsert()
        {
            txtMailContact.Text = txtDescriptionContact.Text = txtDescriptionContact_en.Text = txtDisplayOrderContact.Text = "";
            ddlGroupContact.SelectedIndex = 0;
            btnNewContactGroup.Visible = btnDeleteContactGroup.Visible = false;
            btnSaveContactGroup.CommandName = "Insert";
        }

        protected void imbContactEdit_Command(object sender, CommandEventArgs e)
        {
            lblMsg.Text = "";
            int id = int.Parse(e.CommandArgument.ToString());
            LoadContactDetails(id);
        }

        public void LoadContactDetails(int id)
        {
            XDocument xDocContact = XDocument.Load(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));
            XElement XEContact = xDocContact.Descendants("contact_group").Where(cg => cg.Element("id").Value == id.ToString()).FirstOrDefault();
            txtMailContact.Text = XEContact.Element("mail").Value.Trim();
            txtDescriptionContact.Text = XEContact.Element("showname").Value.Trim();
            txtDescriptionContact_en.Text = XEContact.Element("showname_en").Value.Trim();
            txtDisplayOrderContact.Text = XEContact.Element("index").Value.Trim();
            ddlGroupContact.SelectedValue = XEContact.Element("group").Value.Trim();
            btnSaveContactGroup.CommandArgument = id.ToString();
            btnSaveContactGroup.CommandName = "Update";
            btnNewContactGroup.Visible = btnDeleteContactGroup.Visible = true;
        }

        protected void btnDeleteContactGroup_Click(object sender, EventArgs e)
        {
            XDocument xDocContact = XDocument.Load(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));
            int id = int.Parse(btnSaveContactGroup.CommandArgument);
            XElement XEContact = xDocContact.Descendants("contact_group").Where(cg => cg.Element("id").Value == id.ToString()).FirstOrDefault();

            //create db log
            CreateDBLog(null, null, TypeAction.Delete, "Xoá email hỗ trợ: " + XEContact.Element("mail").Value, "", "", "", "",
                    "", "", "", "");

            XEContact.Remove();
            xDocContact.Save(Server.MapPath(ConfigPaths.Path_ContactGroup_Xml));
            LoadContact();
            ClearContactToInsert();
            lblMsg.Text = "Xoá liên hệ nhóm thành công";
            lblMsg.ForeColor = System.Drawing.Color.Blue;
            //clear outputcache
            HttpResponse.RemoveOutputCacheItem("/Default.aspx");
            HttpResponse.RemoveOutputCacheItem("/Articles.aspx");
            HttpResponse.RemoveOutputCacheItem("/ArticleDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Products.aspx");
            HttpResponse.RemoveOutputCacheItem("/ProductDetails.aspx");
            HttpResponse.RemoveOutputCacheItem("/Contact.aspx");
        }

        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
        string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords)
        {
            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = Membership.GetUser().UserName;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
    }
}